package com.chenbei.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.common.exception.LogicHandleException;
import com.chenbei.system.domain.SysRole;
import com.chenbei.system.mapper.SysRoleMapper;
import com.chenbei.system.service.ISysRoleMenuService;
import com.chenbei.system.service.ISysRoleService;
import com.chenbei.system.service.ISysUserRoleService;
import com.chenbei.system.vo.req.SysRoleBindPermissionReq;
import com.chenbei.system.vo.req.SysRoleReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SysRoleService extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysUserRoleService adminUserRoleRefService;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Override
    public SysRole add(SysRole sysRole) {
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.and(consumer -> {
            consumer.eq(SysRole::getName, sysRole.getName()).or().eq(SysRole::getCode, sysRole.getCode());
        });
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("角色名称或编码已存在");
        }
        if (save(sysRole)) {
            return sysRole;
        } else {
            throw new LogicHandleException("添加失败");
        }
    }


    @Override
    public IPage<SysRole> page(SysRoleReq sysRoleReq) {
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SysRole::getId).ne(SysRole::getId, 1);
        if (StrUtil.isNotBlank(sysRoleReq.getName())) {
            lambdaQueryWrapper.like(SysRole::getName, sysRoleReq.getName());
        }
        if (sysRoleReq.getStatus() != null && sysRoleReq.getStatus() >= 0) {
            lambdaQueryWrapper.eq(SysRole::getStatus, sysRoleReq.getStatus());
        }
        Page<SysRole> page = new Page<>(sysRoleReq.getPageNum(), sysRoleReq.getPageSize());
        return page(page, lambdaQueryWrapper);
    }

    @Override
    public List<SysRole> list(SysRoleReq roleReq) {
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper  = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(SysRole::getId).ne(SysRole::getId, 1);
        if (StrUtil.isNotBlank(roleReq.getName())) {
            lambdaQueryWrapper.like(SysRole::getName, roleReq.getName());
        }
        if (roleReq.getStatus() != null && roleReq.getStatus() >= 0) {
            lambdaQueryWrapper.eq(SysRole::getStatus, roleReq.getStatus());
        }
        return list(lambdaQueryWrapper);
    }


    @Override
    public void update(SysRole sysRole) {
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(SysRole::getId, sysRole.getId())
                .and(consumer -> {
                    consumer.eq(SysRole::getName, sysRole.getName()).or().eq(SysRole::getCode, sysRole.getCode());
                });
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("角色名称或编码已存在");
        }
        updateById(sysRole);
    }

    @Override
    @Transactional
    public void delete(long id) {
        if (id != 1) {
            removeById(id);
            adminUserRoleRefService.removeRoleUsers(id);
        }
    }

    @Override
    public void bindPermissions(SysRoleBindPermissionReq roleBindPermissionReq) {
        sysRoleMenuService.saveOrUpdate(roleBindPermissionReq.getRoleId(), roleBindPermissionReq.getPermissionIds());
    }
}
